--[[
   Copyright (c) 2012 TPV R&D Center Brazil
   All rights reserved
 
   FileName: app.preference
 
   Created by: LPaes
   Date: 01/082012
--]]
------------------------------------------------------------------------
--module table
--Time module, responsible of time, date, sleep
local tPrefenceApp = {}
------------------------------------------------------------------------
--global variables

local nSleep = 1
local nChannPosL = 0
local nChannPosC = 0
local tZapperPref = {}
-- tZapperPref['availableTunerModes'] = {'Air,Cable'}
-- tZapperPref['Password'] = '0000'
-- tZapperPref['OSDTimeOut'] = '5000'
-- tZapperPref['currentLanguage'] = 'En'
-- tZapperPref['currTunerMode'] = 'Air'
-- tZapperPref['lastChanId'] = '0'
-- tZapperPref['parentalLevel'] = 'OFF'
-- tZapperPref['AutoTime'] = 'true'
-- tZapperPref['AutoPowerOff'] = 'true'
-- tZapperPref['BlueScreen'] = 'true'

--[[
-- Picture table, need to insert in database
local tPicAdjust = {standard = {contrast = 55, brightness = 50, color = 60, hue = 0, sharpness = 25, backlight = 100, colorTemp = 'normal', noiseReduc = 'middle'},
                    vivid    = {contrast = 60, brightness = 50, color = 65, hue = 0, sharpness = 30, backlight = 100, colorTemp = 'cool', noiseReduc = 'middle'},
                    mild     = {contrast = 55, brightness = 48, color = 60, hue = 0, sharpness = 25, backlight = 80, colorTemp = 'cool', noiseReduc = 'middle'},
                    cinema   = {contrast = 55, brightness = 50, color = 60, hue = 0, sharpness = 25, backlight = 80, colorTemp = 'normal', noiseReduc = 'middle'},
                    sport    = {contrast = 60, brightness = 48, color = 60, hue = 0, sharpness = 30, backlight = 100, colorTemp = 'cool', noiseReduc = 'middle'}, 
                    game     = {contrast = 60, brightness = 48, color = 60, hue = 0, sharpness = 30, backlight = 100, colorTemp = 'normal', noiseReduc = 'middle'},
                    personal = {contrast = 55, brightness = 48, color = 60, hue = 0, sharpness = 25, backlight = 80, colorTemp = 'cool', noiseReduc = 'middle'},
                    }
--]]

-- Picture table, need to insert in database
-- To Others source, use vivid
local tPicAdjust = {standard = {55, 50, 60, 50, 25, 100, 50, 30},
                    vivid    = {60, 50, 65, 50, 30, 100, 70, 30},
                    mild     = {55, 48, 60, 50, 25, 80, 70, 30},
                    cinema   = {55, 50, 60, 50, 25, 80, 50, 30},
                    sport    = {60, 48, 60, 50, 30, 100, 70, 30},
                    game     = {60, 48, 60, 50, 30, 100, 50, 30},
                    personal = {55, 48, 60, 50, 25, 80, 70, 30},
                   }
--[[
Balance: -50 to 50
Bass:    -10 to 10
Treble:  -10 to 10
-- Audio table: --balance, bass, treble
local tAudioAdjust = {standard = {0, 0, 0},
                      voice    = {0, 0, -3},
                      music    = {0, 4, 2},
                      movie    = {0, 3, -2},
                      theatre  = {0, 2, 1},
                      personal = {0, 0, 0},
                     }
]]

-- Audio table: --balance, bass, treble
local tAudioAdjust = {standard = {50, 50, 50},
                      voice    = {50, 50, 15},
                      music    = {50, 70, 60},
                      movie    = {50, 65, 10},
                      theatre  = {50, 60, 55},
                      personal = {50, 50, 50},
                     }
------------------------------------------------------------------------
--module functions

--- Persist the value of current OSD Language
-- @param sLang String with the new Language 'Pt','En','Es',...
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.init()
   if (control.getSystemPreference('isZapperDBConfigured')[1] ~= 'done') then
      control.setSystemPreference('isZapperDBConfigured','done')
      control.setSystemPreference('availableTunerModes','Air,Cable')
      control.setSystemPreference('Password','0000')
      control.setSystemPreference('OSDTimeOut','5000')
      control.setSystemPreference('currentLanguage','En')
      control.setSystemPreference('currTunerMode','Air')
      control.setSystemPreference('lastChanId','0')
      control.setSystemPreference('parentalLevel','OFF')
      control.setSystemPreference('AutoTime','true')
      control.setSystemPreference('AutoPowerOff','true')
      control.setSystemPreference('BlueScreen','true')
      tZapperPref['availableTunerModes'] = {'Air,Cable'}
      tZapperPref['Password'] = '0000'
      tZapperPref['OSDTimeOut'] = 5000
      tZapperPref['currentLanguage'] = 'En'
      tZapperPref['currTunerMode'] = 'Air'
      tZapperPref['lastChanId'] = 0 
      tZapperPref['parentalLevel'] = 'OFF'
      tZapperPref['AutoTime'] = true
      os.setdatetime("AUTO")
      tZapperPref['AutoPowerOff'] = true
      tZapperPref['BlueScreen'] = true
   else
      local tTempZapperPref = control.getSystemPreferenceList()
      tZapperPref['availableTunerModes'] = tTempZapperPref['availableTunerModes']
      tZapperPref['Password'] = tTempZapperPref['Password'][1]
      tZapperPref['OSDTimeOut'] = tonumber(tTempZapperPref['OSDTimeOut'][1])
      tZapperPref['currentLanguage'] = tTempZapperPref['currentLanguage'][1]
      tZapperPref['currTunerMode'] = tTempZapperPref['currTunerMode'][1]
      tZapperPref['lastChanId'] = tonumber(tTempZapperPref['lastChanId'][1])
      tZapperPref['parentalLevel'] = tTempZapperPref['parentalLevel'][1]
      if(tTempZapperPref['AutoTime'][1] == 'true')then
         tZapperPref['AutoTime'] = true
      else
         tZapperPref['AutoTime'] = false
      end
      if(tTempZapperPref['AutoPowerOff'][1] == 'true')then
         tZapperPref['AutoPowerOff'] = true
      else
         tZapperPref['AutoPowerOff'] = false
      end
      if(tTempZapperPref['BlueScreen'][1] == 'true')then
         tZapperPref['BlueScreen'] = true
      else
         tZapperPref['BlueScreen'] = false
      end
   end
end

--- Persist the value of current OSD Language
-- @param sLang String with the new Language 'Pt','En','Es',...
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.setTunerMode(sMode)
   tZapperPref['currTunerMode'] = sMode
   return control.setSystemPreference('currTunerMode', sMode)
end

--- Restore the value of OSD Language
-- @return String with the persisted Language 'Pt','En','Es',...
function tPrefenceApp.getTunerMode()
   if (tZapperPref['currTunerMode'] == nil) then
     local tRet = control.getSystemPreference('currTunerMode')
     if (type(tRet) == 'table') then
        tZapperPref['currTunerMode'] = tRet[1]
     else
        tZapperPref['currTunerMode'] = tRet
     end
   end
   return tZapperPref['currTunerMode']
end

--- Persist the value of current OSD Language
-- @param sLang String with the new Language 'Pt','En','Es',...
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.setParentalLevel(nLevel)
   tZapperPref['parentalLevel'] = nLevel
   return control.setSystemPreference('parentalLevel', nLevel)
end

--- Restore the value of OSD Language
-- @return String with the persisted Language 'Pt','En','Es',...
function tPrefenceApp.getParentalLevel()
   if (tZapperPref['parentalLevel'] == nil) then
     local tRet = control.getSystemPreference('parentalLevel')
     if (type(tRet) == 'table') then
        tZapperPref['parentalLevel'] = tRet[1]
     else
        tZapperPref['parentalLevel'] = tRet
     end
   end
   return tZapperPref['parentalLevel']
end

--- Persist the value of current OSD Language
-- @param sLang String with the new Language 'Pt','En','Es',...
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.setCurrentLanguage(sLang)
   tZapperPref['currentLanguage'] = sLang
   return control.setSystemPreference('currentLanguage', sLang)
end

--- Restore the value of OSD Language
-- @return String with the persisted Language 'Pt','En','Es',...
function tPrefenceApp.getCurrentLanguage()
   if (tZapperPref['currentLanguage'] == nil) then
      local tRet = control.getSystemPreference('currentLanguage')
      if (type(tRet) == 'table') then
         tZapperPref['currentLanguage'] = tRet[1]
      else
         tZapperPref['currentLanguage'] = tRet
      end
   end
   return tZapperPref['currentLanguage']
end

--- Restore the value of OSD Timeout in ms
-- @return The persisted Timeout 5000,15000,30000,... in ms
function tPrefenceApp.getCurrentPassword()
   if (tZapperPref['Password'] == nil) then
      local tRet = control.getSystemPreference('Password')
      if (type(tRet) == 'table') then
         tZapperPref['Password'] = tRet[1]
      else
         tZapperPref['Password'] = tRet
      end
   end
   return tZapperPref['Password']
end

--- Persist the value of current OSD Timeout in ms
-- @param nTimeOut number with the new Timeout 5000,15000,30000,... in ms
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.setCurrentPassword(sPassword)
   tZapperPref['Password'] = sPassword
   return control.setSystemPreference('Password', sPassword)
end

--- Persist the value of current OSD Timeout in ms
-- @param nTimeOut number with the new Timeout 5000,15000,30000,... in ms
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.setCurrentTimeOut(nTimeOut)
   tZapperPref['OSDTimeOut'] = nTimeOut
   return control.setSystemPreference('OSDTimeOut', nTimeOut)
end

--- Restore the value of OSD Timeout in ms
-- @return The persisted Timeout 5000,15000,30000,... in ms
function tPrefenceApp.getCurrentTimeOut()
   if (tZapperPref['OSDTimeOut'] == nil) then
      local tRet = control.getSystemPreference('OSDTimeOut')
      if (type(tRet) == 'table') then
         tZapperPref['OSDTimeOut'] = tonumber(tRet[1])
      else
         tZapperPref['OSDTimeOut'] = tonumber(tRet)
      end
   end
   return tonumber(tZapperPref['OSDTimeOut'])
end

--- Persist the value of current OSD Timeout in ms
-- @param nSleepTimer position on array - off - 5 - 15 - 30 - 45 - 60 - 90 - 120 - 180 - 240
-- @return boolean True if the command was sent successfully, false if some error occured.
function tPrefenceApp.setCurrentSleepTimer(nSleepTimer)
   nSleep = nSleepTimer
   return true
end

--- Restore the value of OSD Timeout in ms
-- @return nSleepTimer position on array - off - 5 - 15 - 30 - 45 - 60 - 90 - 120 - 180 - 240
function tPrefenceApp.getCurrentSleepTime()
   return nSleep
end

--TODO
function tPrefenceApp.getAutoPowerOff()
   if (tZapperPref['AutoPowerOff'] == nil) then
      local tRet = control.getSystemPreference('AutoPowerOff')
      if (type(tRet) == 'table') then
         if (tRet[1] == 'true') then
            tZapperPref['AutoPowerOff'] = true
         else
            tZapperPref['AutoPowerOff'] = false
         end
      else
         if (tRet == 'true') then
            tZapperPref['AutoPowerOff'] = true
         else
            tZapperPref['AutoPowerOff'] = false
         end
      end
   end
   return tZapperPref['AutoPowerOff']
end

--TODO
function tPrefenceApp.setAutoPowerOff(AutoPowerOff)
   tZapperPref['AutoPowerOff'] = AutoPowerOff
   return control.setSystemPreference('AutoPowerOff', AutoPowerOff)
end


--TODO
function tPrefenceApp.getAutoTime()
   if (tZapperPref['AutoTime'] == nil) then
      local tRet = control.getSystemPreference('AutoTime')
      if (type(tRet) == 'table') then
         if (tRet[1] == 'true') then
            tZapperPref['AutoTime'] = true
         else
            tZapperPref['AutoTime'] = false
         end
      else
         if (tRet == 'true') then
            tZapperPref['AutoTime'] = true
         else
            tZapperPref['AutoTime'] = false
         end
      end
   end
   return tZapperPref['AutoTime']
end

--TODO
function tPrefenceApp.setAutoTime(AutoTime)
   tZapperPref['AutoTime'] = AutoTime
   return control.setSystemPreference('AutoTime', AutoTime)
end


--TODO
function tPrefenceApp.getBlueScreen()
   if (tZapperPref['BlueScreen'] == nil) then
      local tRet = control.getSystemPreference('BlueScreen')
      if (type(tRet) == 'table') then
         if (tRet[1] == 'true') then
            tZapperPref['BlueScreen'] = true
         else
            tZapperPref['BlueScreen'] = false
         end
      else
         if (tRet == 'true') then
            tZapperPref['BlueScreen'] = true
         else
            tZapperPref['BlueScreen'] = false
         end
      end
   end
   return tZapperPref['BlueScreen']
end

--TODO
function tPrefenceApp.setBlueScreen(BlueScreen)
   tZapperPref['BlueScreen'] = BlueScreen
   return control.setSystemPreference('BlueScreen', BlueScreen)
end


--TODO
function tPrefenceApp.getLastChanId()
   if (tZapperPref['lastChanId'] == nil) then
      local tRet = control.getSystemPreference('lastChanId')
      if (tRet[1]) then
         tZapperPref['lastChanId'] = tRet[1]
      else
         tZapperPref['lastChanId'] = tRet
      end
   end
   return tZapperPref['lastChanId']
end

--TODO
function tPrefenceApp.setLastChanId(lastChanId)
   tZapperPref['lastChanId'] = lastChanId
   return control.setSystemPreference('lastChanId', lastChanId)
end


--TODO
function tPrefenceApp.getChannListPosition()
   return nChannPosL, nChannPosC
end

--TODO
function tPrefenceApp.setChannListPosition(nPositionLine, nPositionCol)
   nChannPosL = nPositionLine
   nChannPosC = nPositionCol
   return true
end
                    
-- Function to return on of predefined mode in the table tPicAdjust
-- return a table
function tPrefenceApp.getPicureAdjustMode(nMode)
   local sNameMode
   if (nMode == 1) then
      sNameMode = 'standard'
   elseif (nMode == 2) then
      sNameMode = 'vivid'
   elseif (nMode == 3) then
      sNameMode = 'mild'
   elseif (nMode == 4) then
      sNameMode = 'cinema'
   elseif (nMode == 5) then
      sNameMode = 'sport'
   elseif (nMode == 6) then
      sNameMode = 'game'
   elseif (nMode == 7) then
      sNameMode = 'personal'
   end

   return tPicAdjust[sNameMode]
end

-- Function to return on of predefined mode in the table tAudioAdjust
-- return a table
function tPrefenceApp.getAudioAdjustMode(nMode)
   local sNameMode
   if (nMode == 1) then
      sNameMode = 'standard'
   elseif (nMode == 2) then
      sNameMode = 'voice'
   elseif (nMode == 3) then
      sNameMode = 'music'
   elseif (nMode == 4) then
      sNameMode = 'movie'
   elseif (nMode == 5) then
      sNameMode = 'theatre'
   elseif (nMode == 6) then
      sNameMode = 'personal'
   end

   return tAudioAdjust[sNameMode]
end

------------------------------------------------------------------------
return tPrefenceApp
